package cn.wit.summit.chwan.adapters;

import android.app.Activity;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.dto.GameInfoDetialV2Bean;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.JumpBaseBean;
import cn.wit.summit.chwan.models.response.GreatCrackCollectionResponse;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.widgets.ItemListViewV2;

/**
 * Created by Alan on 2018/7/19.
 */

public class GreatCrackAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private List<ViewBean> mDatas;
    private Activity mContext;
    private LayoutInflater inflater;
    private Map<String, String> downTemp = new HashMap<>();

    public GreatCrackAdapter(Activity context, List<ViewBean> datas) {
        this.mContext = context;
        this.mDatas = datas;
        inflater = LayoutInflater.from(mContext);
    }

    public String getPoByGameId(String gameId) {
        String result = "";
        if (downTemp.size()>0 && downTemp.containsKey(gameId)){
            result = downTemp.get(gameId);
        }
        return result;
    }

    public GameListBaseBean getBeanData(int itemPo, int po) {
        if (mDatas.get(itemPo).getViewType() == ViewType.TYPE_GAME) {
            GameListBaseBean tempJump = (GameListBaseBean) mDatas.get(itemPo).getObj();
            return tempJump;
        }
        return null;
    }

    public void clearTempData () {
        downTemp.clear();
    }

    public enum ViewType {
        TYPE_TOP,//头部
        TYPE_GAME,//游戏信息
        TYPE_GAME_CRACK,//游戏破解须知
        TYPE_LINE//分割线
    }

    public static class ViewBean {
        ViewType viewType;
        Object obj;

        public ViewBean() {
        }

        public ViewBean(ViewType viewType) {
            this.viewType = viewType;
            this.obj = null;
        }

        public ViewBean(ViewType viewType, Object obj) {
            this.viewType = viewType;
            this.obj = obj;
        }

        public ViewType getViewType() {
            return viewType;
        }

        public void setViewType(ViewType viewType) {
            this.viewType = viewType;
        }

        public Object getObj() {
            return obj;
        }

        public void setObj(Object obj) {
            this.obj = obj;
        }
    }

    //设置ITEM类型，可以自由发挥，这里设置item position单数显示item1 偶数显示item2
    @Override
    public int getItemViewType(int position) {
        if (mDatas != null) {
            return mDatas.get(position).getViewType().ordinal();
        }
        return -1;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;

        //加载Item View的时候根据不同TYPE加载不同的布局
        if (viewType == ViewType.TYPE_TOP.ordinal()) {
            holder = new TopHolder(inflater.inflate(R.layout.item_great_crack_top_view, parent, false));
        } else if (viewType == ViewType.TYPE_GAME.ordinal()) {
            holder = new GameInfoHolder(inflater.inflate(R.layout.item_great_crack_game_info_view, parent, false));
        } else if (viewType == ViewType.TYPE_LINE.ordinal()) {
            holder = new LineHolder(inflater.inflate(R.layout.item_great_crack_line_view, parent, false));
        } else if (viewType == ViewType.TYPE_GAME_CRACK.ordinal()) {
            holder = new CrackInfoHolder(inflater.inflate(R.layout.item_great_crack_crack_info_view, parent, false));
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ViewBean viewBean = mDatas.get(position);
        if (holder instanceof TopHolder) {
            final GreatCrackCollectionResponse temp = (GreatCrackCollectionResponse) viewBean.getObj();
            TopHolder topHolder = (TopHolder) holder;

            String bgUrl = temp.getCollection_cover_pic();
            if (!StringUtils.isEmpty(bgUrl))
                topHolder.itemCrackGameTopBgIv.setImageURI(bgUrl);
            topHolder.itemGreatCrackTitleTv.setText(temp.getCollection_title());
            topHolder.itemGreatCrackSubTitleTv.setText(temp.getCollection_info());
        } else if (holder instanceof GameInfoHolder) {
            GameInfoDetialV2Bean temp = (GameInfoDetialV2Bean) viewBean.getObj();
            GameInfoHolder gameInfoHolder = (GameInfoHolder) holder;

            downTemp.put(temp.getGame_id(), position+"");
            gameInfoHolder.itemGreatCrackGameInfoView.setData(mContext, temp);
        } else if (holder instanceof LineHolder) {
        } else if (holder instanceof CrackInfoHolder) {
            final GameInfoDetialV2Bean temp = (GameInfoDetialV2Bean) viewBean.getObj();
            CrackInfoHolder crackInfoHolder = (CrackInfoHolder) holder;

            String strTemp = "MOD描述：";
            SpannableString spannableString = new SpannableString(strTemp);
            //设置颜色
            spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#3b3b3b")), 0, strTemp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            //设置字体，BOLD为粗体
            spannableString.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0, strTemp.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

            crackInfoHolder.crackInfoTx.setText(spannableString+temp.getGame_explain());
            crackInfoHolder.crackInfoRootFl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    IntentUtil.getInstance().goGamedetial(mContext, temp.getGame_id());
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    public class TopHolder extends RecyclerView.ViewHolder {
        SimpleDraweeView itemCrackGameTopBgIv;
        TextView itemGreatCrackTitleTv;
        TextView itemGreatCrackSubTitleTv;

        public TopHolder(View view) {
            super(view);
            itemCrackGameTopBgIv = view.findViewById(R.id.itemCrackGameTopBgIv);
            itemGreatCrackTitleTv = view.findViewById(R.id.itemGreatCrackTitleTv);
            itemGreatCrackSubTitleTv = view.findViewById(R.id.itemGreatCrackSubTitleTv);
        }
    }

    public class GameInfoHolder extends RecyclerView.ViewHolder {
        FrameLayout itemGreatCrackGameInfoViewRootFl;
        ItemListViewV2 itemGreatCrackGameInfoView;

        public GameInfoHolder(View view) {
            super(view);
            itemGreatCrackGameInfoViewRootFl = view.findViewById(R.id.itemGreatCrackGameInfoViewRootFl);
            itemGreatCrackGameInfoView = view.findViewById(R.id.itemGreatCrackGameInfoView);
        }
    }

    public class LineHolder extends RecyclerView.ViewHolder {

        public LineHolder(View view) {
            super(view);
        }
    }

    public class CrackInfoHolder extends RecyclerView.ViewHolder {
        FrameLayout crackInfoRootFl;
        TextView crackInfoTx;

        public CrackInfoHolder(View view) {
            super(view);
            crackInfoRootFl = view.findViewById(R.id.crackInfoRootFl);
            crackInfoTx = view.findViewById(R.id.crackInfoTx);
        }
    }
}
